package hikari.gui.panels;

import com.gooddies.swing.hCheckBox;
import com.gooddies.swing.hComboBox;
import com.gooddies.wiring.annotations.PostWiring;
import com.gooddies.wiring.annotations.Wire;
import com.gooddies.wiring.annotations.WiringComponent;
import hikari.gui.MainWindow;
import hikari.gui.animationeditor.AnimationEditorWindow;
import hikari.gui.guicomponents.hLabel;
import hikari.hcomponents.HComponent;
import hikari.managers.AnimationSettingStore;
import hikari.objects3d.Custom3DObject;
import hikari.objects_data.Model3dData;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import net.miginfocom.swing.MigLayout;

/**
 * @author sad
 */
@WiringComponent(singleton = true, lazy = true)
@SuppressWarnings("unchecked")
public class AnimationEditPanel extends HComponent {

    private Custom3DObject model;
    private hComboBox animationSegmentsCB;
    private hCheckBox animationImmidiatelyEnabled;
    private hCheckBox animationLoop;
    private boolean disableEvents = false;
    @Wire
    private AnimationSettingStore animationSettingStore;
    @Wire
    private MainWindow mainWindow;
    private AnimationEditPanel() {
    }

    @PostWiring
    private void init() {
        setLayout(new MigLayout("insets 5"));
        setTitle("Animation Settings");
        JButton animationEditButton = new JButton("Edit Animation");
        add(animationEditButton, "wrap");

        animationEditButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AnimationEditorWindow window = new AnimationEditorWindow(mainWindow, AnimationEditPanel.this.model.getRelativeModelPath());
                window.setVisible(true);
                update();
            }
        });

        animationSegmentsCB = new hComboBox();
        animationSegmentsCB.setPreferredSize(new Dimension(100, 20));
        animationSegmentsCB.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (disableEvents == true) {
                    return;
                }
                if (e.getStateChange() != ItemEvent.SELECTED) {
                    return;
                }
                Object item = animationSegmentsCB.getSelectedItem();
                if (item != null) {
                    String trac = item.toString();
                    if (trac.trim().isEmpty()) {
                        trac = null;
                    }
                    model.setSelectedAnimationTrac(trac);
                }
            }
        });
        add(new hLabel("Animation Segments"), "wrap");
        add(animationSegmentsCB, "wrap");

        animationImmidiatelyEnabled = new hCheckBox();
        animationImmidiatelyEnabled.setPreferredSize(new Dimension(100, 20));
        animationImmidiatelyEnabled.setText("Start immidiately");
        animationImmidiatelyEnabled.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (disableEvents == true) {
                    return;
                }
                setImmidiatelyAnimated(animationImmidiatelyEnabled.isSelected());
            }
        });

        animationLoop = new hCheckBox();
        animationLoop.setPreferredSize(new Dimension(100, 20));
        animationLoop.setText("Loop animation");
        animationLoop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (disableEvents == true) {
                    return;
                }
                setLoopMode(animationLoop.isSelected());
            }
        });

        add(animationImmidiatelyEnabled, "wrap");
        add(animationLoop, "wrap");
    }

    public void setModel(Custom3DObject model) {
        this.model = model;
    }

    public void setImmidiatelyAnimated(boolean enabled) {
        model.setImmidiatelyAnimated(enabled);
    }

    public void setLoopMode(boolean enabled) {
        model.getNode().setLoopMode(enabled);
    }

    public void update() {
        disableEvents = true;
        if (!isExpanded()) {
            return;
        }
        animationSegmentsCB.removeAllItems();
        Model3dData animationData = animationSettingStore.getDataForAnimationModel(model.getRelativeModelPath());
        animationSegmentsCB.addItem("");
        int index = 1;
        for (String aName : animationData.getAnimationNames()) {
            animationSegmentsCB.addItem(aName);
            if (aName.equals(model.getSelectedAnimationTrac())) {
                animationSegmentsCB.setSelectedIndex(index);
            }
            index++;
        }
        animationLoop.setSelected(model.getNode().getLoopMode());
        animationImmidiatelyEnabled.setSelected(model.isImmidiatelyAnimated());
        disableEvents = false;
    }
}
